package com.sale.succ.utils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;

import eve.util.FormattedDataSource;

/**
 * This is a bundle class for all String-resources.
 * 
 */
public final class ProjectBundle {
	private static HashMap cache = new HashMap();
	private static Locale currLocale = new Locale("ru", "RU");

	public static String getValue(String bundlePath, String key) {
		if (!cache.containsKey(bundlePath)) {
			cache.put(bundlePath, createProp(bundlePath, currLocale));
		}
		return ((Properties) cache.get(bundlePath)).get(key).toString();
	}

	private static Properties createProp(String bundlePath, Locale locale) {
		Properties prop = new Properties();
		try {
			FormattedDataSource fds = new FormattedDataSource();
			fds.set(createPath(bundlePath, locale));
			prop.load(fds.getInputStream());
			fds.close();
		} catch (Exception e) {
			Logger.error(e.getMessage());
		}
		return prop;
	}

	private static String createPath(String bundlePath, Locale locale) {
		String result = SystemConstants.DEFAULT_PATH + bundlePath + "_"
				+ locale.getLanguage();
		if (locale.getCountry() != null) {
			if (locale.getCountry().length() != 0) {
				result = result + "_" + locale.getCountry();
			}
		}
		return result + ".properties";

	}
}
